﻿public class Solution {
    
    // 映射表
    private string[] letterMap = 
    {
        "", // 0
        "", // 1
        "abc", // 2
        "def", // 3
        "ghi", // 4
        "jkl", // 5
        "mno", // 6
        "pqrs", // 7
        "tuv", // 8
        "wxyz", // 9
    };

    private IList<string> result = new List<string>();
    private List<char> res = new List<char>();
    
    public IList<string> LetterCombinations(string digits)
    {
        if (digits is not null && digits.Length > 0 ) trackbacking(digits, 0);

        return result;
    }

    private void trackbacking(string digits, int ind)
    {
        if (ind >= digits.Length)
        {
            string s = new string(res.ToArray());
            result.Add(s);
            return;
        }

        int index = digits[ind] - '0';
        string letter = letterMap[index];
        for (int i = 0; i < letter.Length; i++)
        {
            res.Add(letter[i]);
            trackbacking(digits, ind + 1);
            res.RemoveAt(res.Count - 1);
        }
    }

    public static void Main(string[] args)
    {
        string digits = "23";

        Solution solution = new Solution();
        IList<string> result = solution.LetterCombinations(digits);
        foreach (string res in result)
        {
            Console.WriteLine(res);
        }
    }
}